package com.neofelis.autohome;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 汤姆汉克斯 on 2017/3/29.
 */
public class crawler {
    private static Logger logger = LoggerFactory.getLogger(crawler.class);

    public static void main(String[] args) {
        Map<String, String> areas = crawlAreaList();
        System.out.println("采集到城市数量: " + areas.size());
        if (Integer.parseInt(args[0]) > 0) {
            for (String areaUrl : areas.keySet()) {
                String areaName = areas.get(areaUrl);//得到每个key多对用value的值
                System.out.println(areaName + "     " + areaUrl);
                if (Integer.parseInt(args[0]) > 1) {
                    //采集经销商入口
                    String dealerListUrl = crawlDealer(areaUrl.toString());
                    if (dealerListUrl == null) {
                        continue;
                    }
                    //采集经销商列表
                    Map<String, String> dealerList = crawlDealerList(dealerListUrl);
                    if (dealerList == null) {
                        continue;
                    }
                    System.out.println(areaName + "经销商数量: " + dealerList.size());

                    //采集经销商报价数据
                    for (String dealerUrl : dealerList.keySet()) {
                        System.out.println(" 地区：" + areaName + ", 经销商：" + dealerList.get(dealerUrl));
                        if (Integer.parseInt(args[0]) > 2) {
                            crawlData(dealerUrl);
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
        }
/**
 *test
 */
//        crawlDealerList("http://dealer.autohome.com.cn/yi_chun/");
//
//
    }

    /**
     * 采集各个城市的地址入口
     *
     * @return
     */
    public static Map<String, String> crawlAreaList() {
        Map<String, String> arearList = new HashMap<String, String>();
        String initUrl = "http://www.autohome.com.cn/AreaList.html";
        Document doc = null;
        try {
            doc = Jsoup.connect(initUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        Elements links = doc.getElementsByTag("a");
        for (Element link : links) {
            String linkHref = link.attr("href");
            String linkText = link.text();

            String regEx = "http.*/cheshi/";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(linkHref);
            if (m.find()) {
//                System.out.println(linkHref + " " + linkText);
                arearList.put(linkHref, linkText);
            }
        }

        return arearList;
    }

    /**
     * 进入城市主页，找到经销商入口
     *
     * @param areaUrl
     */
    public static String crawlDealer(String areaUrl) {

        Document doc = null;
        try {
            doc = Jsoup.connect(areaUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e2) {
                e.printStackTrace();
            }
            return null;
        }

        Elements navlinks = doc.getElementsByClass("nav-channel-link").get(0).getElementsByTag("a");
        String linkHref = navlinks.get(0).attr("href");
        String linkText = navlinks.get(0).text();
//        System.out.println(linkHref + "  " + linkText);
//        String[][] dealer = new String[][]{{linkHref}, {linkText}};
        return linkHref;

    }

    /**
     * 采集某地区经销商列表
     *
     * @param dealearUrl
     */
    public static Map<String, String> crawlDealerList(String dealearUrl) {
        Map<String, String> dealearList = new HashMap<String, String>();
        Document doc = null;
        ArrayList<String> pagelist = new ArrayList<String>();
        try {
            doc = Jsoup.connect(dealearUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("Error: " + dealearUrl + " 经销商采集失败");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e2) {
                e.printStackTrace();
            }
            return null;
        }
        //分页
        if (pagelist.isEmpty()) {
            String pinyin = "";
            int dealerCount = 0;
            String regEx = "pinyin\\s?=\\s?\"(\\w+)\";";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(doc.data());
            if (m.find()) pinyin = m.group(1);

            String regEx2 = "dealerCount\\s?=\\s?([0-9]+);";
            Pattern p2 = Pattern.compile(regEx2);
            Matcher m2 = p2.matcher(doc.data());
            if (m2.find()) dealerCount = Integer.parseInt(m2.group(1));
            for (int i = 1; i <= dealerCount; i++) {
                String link_url = "http://dealer.autohome.com.cn" + "/" + pinyin + "?countyId=" + 0 + "&brandId=" + 0 + "&seriesId=" + 0 +
                        "&factoryId=" + 0 + "&pageIndex=" + i + "&kindId=" + 1 + "&orderType=" + 0;
                pagelist.add(link_url);

            }
        }

        for (String pageurl : pagelist) {
            Document pagedoc = null;
            try {
                pagedoc = Jsoup.connect(pageurl).get();
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("Error: " + pageurl + " 采集失败");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e2) {
                    e.printStackTrace();
                }
                continue;
            }
//            System.out.println(pageurl);
            Elements dealerLinks = null;
            try {
                dealerLinks = pagedoc.getElementsByClass("list-box").get(0).getElementsByClass("tit-row");
            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
                continue;
            }

            for (Element link : dealerLinks) {
//            System.out.println(link);
                String linkHref = link.getElementsByTag("a").attr("href");
                String linkText = link.getElementsByTag("a").text();
                //直接通过最后一个/后面的内容替换成price.html就是报价页面 ，目前的规律是 2017年3月29日
                String tempHref = "http:" + linkHref.split("#")[0] + "price.html";
//                System.out.println(tempHref);
                dealearList.put(tempHref, linkText);
            }
        }
        return dealearList;
    }

    /**
     * 根据经销商的报价页面采集当前经销商的所有车辆报价
     *
     * @param priceUrl
     */
    public static void crawlData(String priceUrl) {
        Document doc = null;
        try {
            doc = Jsoup.connect(priceUrl).get();
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("Errot Url: " + priceUrl);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e2) {
                e.printStackTrace();
            }
            return;
        }
        Elements carList = doc.getElementsByClass("price-dl");

        for (Element priceList : carList) {
            String carDes = priceList.getElementsByClass("name").text();
            System.out.println("汽车品牌：" + carDes);
            Elements tables = priceList.getElementsByClass("list-table");
            for (Element table : tables) {
                Elements trs = table.getElementsByTag("tr");
                for (Element tr : trs) {
                    System.out.println(tr.text());
                }
            }
        }

    }
}
